package com.situ.crm.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.situ.crm.common.ServerResponse;
import com.situ.crm.entity.Permission;
import com.situ.crm.entity.Role;
import com.situ.crm.entity.User;
import com.situ.crm.entity.UserRole;
import com.situ.crm.mapper.PermissionMapper;
import com.situ.crm.mapper.RoleMapper;
import com.situ.crm.mapper.UserMapper;
import com.situ.crm.mapper.UserRoleMapper;
import com.situ.crm.service.IUserService;
import com.situ.crm.util.UserContext;
import com.situ.crm.vo.LayUISelectMVO;
@Service
public class UserServiceImpl implements IUserService {
	@Autowired
	private UserMapper userMapper;
	@Autowired
	private UserRoleMapper userRoleMapper;
	@Autowired
	private RoleMapper roleMapper;
	@Autowired
	private PermissionMapper permissionMapper;
	
	@Override
	public ServerResponse pageList(Integer page, Integer limit, User user) {
		//开启分页
		PageHelper.startPage(page,limit);
		//查询
		List<User> list = userMapper.pageList(user);
		//封装pageInfo
		PageInfo pageInfo = new PageInfo<>(list);
		//拿到总数据
		Integer count = (int) pageInfo.getTotal();
		return ServerResponse.createSuccess("查找成功", count, list);
	}

	//添加功能，插入user表和user_role表
	@Override
	public ServerResponse insert(User user, String roles) {
		try {
			Integer count = userMapper.insert(user);
			
			String[] roleIds = roles.split(",");
			for (String roleId : roleIds) {
				UserRole userRole = new UserRole(user.getId(),Integer.parseInt(roleId));
				userRoleMapper.insert(userRole);
			}
			return ServerResponse.createSuccess("插入成功");
		} catch (Exception e) {
			return ServerResponse.createSuccess("插入失败");
		}
		
	}

	//根据Id删除
	@Override
	public ServerResponse deleteById(Integer id) {
		try {
			userRoleMapper.deleteByUserId(id);
			Integer count = userMapper.deleteByPrimaryKey(id);
			if (count == 1) {
				return ServerResponse.createSuccess("删除成功");
			} else {
				return ServerResponse.createError("删除失败");
			}
		} catch (Exception e) {
			return ServerResponse.createError("删除失败");
		}
	}

	//批量删除
	@Override
	public ServerResponse deleteByAll(String ids) {
		String[] idArrays = ids.split(",");
		try {
			userRoleMapper.deleteAllByUserId(idArrays);
			Integer count = userMapper.deleteAll(idArrays);
			if (count >= 1) {
				return ServerResponse.createSuccess("删除成功");
			} else {
				return ServerResponse.createError("删除失败 ");
			}
		} catch (Exception e) {
			return ServerResponse.createError("删除失败 ");
		}
	}

	//修改前查询
	@Override
	public ServerResponse selectUserAndRole(Integer userId) {
		Map<String, Object> map = new HashMap<>();
		
		User user = userMapper.selectByPrimaryKey(userId);
		map.put("user", user);
		
		List<Role> roles = roleMapper.pageList(new Role());
		List<LayUISelectMVO> list = new ArrayList<>();
		for (Role role : roles) {
			LayUISelectMVO layUISelectMVO = new LayUISelectMVO();
			layUISelectMVO.setId(role.getId());
			layUISelectMVO.setName(role.getName());
			layUISelectMVO.setStatus(1);
			list.add(layUISelectMVO);
		}
		map.put("roles", list);
		
		List<Integer> userSelectRole = userRoleMapper.userSelectRole(userId);
		map.put("selectIds", userSelectRole.toArray());
		return ServerResponse.createSuccess("成功", map);
	}

	//修改
	@Override
	public ServerResponse update(User user, String roles) {
		try {
			Integer count = userMapper.updateByPrimaryKeySelective(user);
			
			userRoleMapper.userIdDeleteRoleId(user.getId());
			
			String[] roleIds = roles.split(",");
			for (String roleId : roleIds) {
				UserRole userRole = new UserRole(user.getId(),Integer.parseInt(roleId));
				userRoleMapper.insert(userRole);
			}
			return ServerResponse.createSuccess("修改成功");
			
		} catch (Exception e) {
			return ServerResponse.createSuccess("修改失败");
		}
	}

	//登陆
	@Override
	public ServerResponse login(String name, String password,HttpSession session) {
		//用户存不存在
		int count = userMapper.checkUserName(name);
		if (count == 0) {
			return ServerResponse.createError("用户不存在");
		}
		
		//走到这说明用户名存在
		UserContext.session = session;
		User user = userMapper.selectLogin(password,name);
		if (user != null) {
			//当前用户放入session
			session.setAttribute(UserContext.USER_IN_SESSION, user);
			//查询用户拥有的权限
			List<Permission> permissions = permissionMapper.selectByUserId(user.getId());
			session.setAttribute(UserContext.PERMISSION_IN_SESSION, permissions);
			return ServerResponse.createSuccess("登陆成功");
		}
		
		return ServerResponse.createError("登陆失败");
	}

	//分配只显示是销售人员
	@Override
	public List<User> selectXiaoshouUser() {
		return userMapper.selectXiaoShouUSer() ;
	}
}
